/* Copyright (c) 2008 LepomisLab.com, Andrey Turkin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "NSDictionary.h"
#include "NSKeyedUnarchiver.h"

IMPLEMENT_DYNAMIC_CLASS(NSDictionary,NSObject)

using namespace std;

void NSDictionary::delete_map(void)
{
	for(dictionary_map::iterator i = _map->begin();i != _map->end();i++)
	{
		delete i->second;
	}
	delete _map;
	_map = NULL;
}

NSDictionary::NSDictionary(wxString *keys[], wxObject* values[], size_t len) : NSObject()
{
	_map = new dictionary_map();
	for(size_t l = 0; l < len; l++)
	{
		wxASSERT(keys[l]);
		wxASSERT(values[l]);
		_map->insert(dictionary_map::value_type(*keys[l], values[l]));
	}
	delete keys;
	delete values;
}

NSDictionary::NSDictionary(wxString *key, wxObject* value) : NSObject()
{
	_map = new dictionary_map();
	wxASSERT(key);
	wxASSERT(value);
	_map->insert(dictionary_map::value_type(*key, value));
}

NSDictionary::NSDictionary() : NSObject()
{
	_map = new dictionary_map();
}

wxObject *NSDictionary::getObject(const wxString &key)
{
	dictionary_map::iterator i;
	i = _map->find(key);
	if (i==_map->end())
		return NULL;
	wxObject *obj = i->second;
//	wxASSERT(obj);
	return obj;
}

void NSDictionary::setObject(const wxString &key, wxObject* value)
{
	dictionary_map::iterator i;
	i = _map->find(key);
	_willValueChangedForKey(key);
	if (i==_map->end())
		Insert(key, value);
	else
		i->second = value;
	_didValueChangedForKey(key);
}

void NSDictionary::initWithUnarchiver(NSKeyedUnarchiver *archiver)
{
	wxObject *_k = archiver->decodeArrayObjectForKey(wxT("NS.keys"));
	if (_k)
	{
		NSArray *keys = wxDynamicCast(_k, NSArray);
		wxASSERT(keys);
		NSArray *values = wxDynamicCast(archiver->decodeArrayObjectForKey(wxT("NS.objects")), NSArray);
		wxASSERT(values);
		size_t len = keys->length();
		for(size_t l = 0; l < len; l++)
		{
			wxVariant*key = wxDynamicCast(keys->getObject(l), wxVariant);
			wxObject *val = values->getObject(l);
			wxASSERT(key);
			wxASSERT(val);
			_map->insert(dictionary_map::value_type(key->GetString(), val));
		}
	} else {
		_map = new dictionary_map();
		for(size_t l = 0; ; l++)
		{
			wxString key_n;
			key_n.Format(wxT("NS.object.%u"), l);
			wxObject *val = archiver->decodeObjectForKey(key_n);
			if (!val)
				break;
			key_n.Format(wxT("NS.key.%u"), l);
			wxVariant *key = wxDynamicCast(archiver->decodeObjectForKey(key_n), wxVariant);
			wxASSERT(key);
			_map->insert(dictionary_map::value_type(key->GetString(), val));
		}
	}
}

wxObject* NSDictionary::_getValue(const wxString& key)
{
	wxObject *o = getObject(key);
	return o;
}

void NSDictionary::_setValue(const wxString& key, wxObject* value)
{
	setObject(key, value);
}

void NSDictionary::Insert(const wxString& key, wxObject* value)
{
	_map->insert(dictionary_map::value_type(key, value));
}

void NSDictionary::Erase(const wxString& key)
{
	dictionary_map::iterator i = _map->find(key);
	if (i != _map->end())
		_map->erase(i);
}

void NSDictionary::Merge(NSDictionary* from)
{
	for(dictionary_map::iterator i = from->_map->begin(); i != from->_map->end(); i++)
	{
		setObject(i->first, i->second);
	}
	from->_map->clear();
}
